package com.dkd.common.utils;


import com.dkd.common.core.domain.Location;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import org.springframework.stereotype.Component;

@Component
public class LocationUtils {
    private static final String API_KEY = "bed70d292ecfb424fd4812f8363423a2"; // 替换为你的高德API Key
    private static final String GEO_API_URL = "https://restapi.amap.com/v3/geocode/geo";

    public static Location getLocation(String  address) {
        String location = getLocationByAddress(address);
        if (location != null) {
            System.out.println("地址：" + address + " 的经纬度为：" + location);
        } else {
            System.out.println("无法获取地址的经纬度");
        }
        String[] split = location.split(",");
        Location location1 = new Location();
        location1.setLongitudes(split[0]); //经度
        location1.setLatitude(split[1]); //纬度
        return location1;
    }

    public static String getLocationByAddress(String address) {
        String url = GEO_API_URL + "?address=" + address + "&key=" + API_KEY;
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpGet httpGet = new HttpGet(url);
            HttpResponse response = httpClient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                String result = EntityUtils.toString(entity);
                System.out.println("API Response: " + result); // 添加这行来查看实际返回数据
                JSONObject jsonObject = new JSONObject(result);
                if (jsonObject.getInt("status") == 1) {
                    // 修复：检查 geocodes 是否为 JSONArray
                    if (jsonObject.has("geocodes")) {
                        Object geocodesObj = jsonObject.get("geocodes");
                        if (geocodesObj instanceof JSONArray) {
                            JSONArray geocodesArray = (JSONArray) geocodesObj;
                            if (geocodesArray.length() > 0) {
                                JSONObject geocode = geocodesArray.getJSONObject(0);
                                if (geocode.has("location")) {
                                    return geocode.getString("location");
                                }
                            }
                        } else if (geocodesObj instanceof JSONObject) {
                            JSONObject geocodes = (JSONObject) geocodesObj;
                            if (geocodes.has("location")) {
                                return geocodes.getString("location");
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }
}
